﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Epam.Exceptions.RepositoryExceptions;
using Epam.PeopleInterfaces;

namespace Epam.PeopleRepository
{
    public class NewsSqlRepository:CrossEntityRepository,INewsRepository
    {
        public void Create(INewsEntity item)
        {
            try
            {
                News.Add(FromBusinessToDtoMapper.Convert(item));
                SaveChanges();
            }
            catch (Exception e)
            {
                throw new CrudOperationException("Create operation failed. " + e.Message);
            } 
           
        }

        public void Update(INewsEntity item)
        {
            try
            {
                DtoNewsEntity temp = (from dtoNewsEntity in News
                                      where item.Id == dtoNewsEntity.Id
                                      select dtoNewsEntity).FirstOrDefault();
                if (temp != null)
                {
                    temp.Text = item.Text;
                    temp.Title = item.Title;
                    temp.Created = item.Created;
                    SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw new CrudOperationException("Update operation failed. " + e.Message);
            } 
           
        }

        public void Delete(Guid id)
        {
            try
            {
                DtoNewsEntity temp = (from dtoNewsEntity in News
                                      where dtoNewsEntity.Id == id
                                      select dtoNewsEntity).FirstOrDefault();
                if (temp != null)
                {
                    News.Remove(temp);
                    SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw new CrudOperationException("Delete operation failed. "+e.Message);
            }
        }

        public IDtoNewsEntity GetById(Guid id)
        {
            try
            {
                return (from dtoNewsEntity in News
                        where dtoNewsEntity.Id == id
                        select dtoNewsEntity).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw new FindOperationException("GetById operation failed. "+e.Message);
            }
            
        }

        public IEnumerable<IDtoNewsEntity> ReadAll()
        {
            try
            {
                return News.ToList();
            }
            catch (Exception e)
            {
                throw new FindOperationException("ReadAll operation failed. " + e.Message);
            }
            
        }

        public IEnumerable<IDtoNewsEntity> ReadPage(int currentPage, int itemPerPage)
        {
            try
            {
                if (currentPage < 0 || itemPerPage <= 0) return new Collection<IDtoNewsEntity>();
                return News.OrderBy(item => item.Created).Skip(currentPage * itemPerPage).Take(itemPerPage);
            }
            catch (Exception e)
            {
                throw new FindOperationException("ReadPage operation failed. " + e.Message);
            }
           
        }
    }
}
